package org.zhao.Share;

import java.util.Scanner;

/**
 * @classname:SharedBikeMgr
 * @description:
 * @author: Mr.Z
 * @date: 2020/11/29 20:49
 * @version: 0.1
 * @since:1.8
 */
public class SharedBikeMgr {
    public static SharedBikeCompany[] companies = new SharedBikeCompany[3];
    Scanner input = new Scanner(System.in);

    static {

        SharedBike[] ofo = new SharedBike[20];
        SharedBike ofo0 = new SharedBike(1, "ofo单车0", 1, null);
        SharedBike ofo1 = new SharedBike(2, "ofo单车1", 1, null);
        SharedBike ofo2 = new SharedBike(3, "ofo单车2", 1, null);
        ofo[0] = ofo0;
        ofo[1] = ofo1;
        ofo[2] = ofo2;
        SharedBikeCompany companyofo = new SharedBikeCompany(1, "ofo单车", ofo, 3, 100);


        SharedBike[] halo = new SharedBike[20];
        SharedBike halo0 = new SharedBike(1, "halo单车0", 1, null);
        SharedBike halo1 = new SharedBike(2, "halo单车1", 1, null);
        SharedBike halo2 = new SharedBike(3, "halo单车2", 1, null);
        halo[0] = halo0;
        halo[1] = halo1;
        halo[2] = halo2;
        SharedBikeCompany companyhalo = new SharedBikeCompany(2, "halo单车", halo, 3, 210);


        SharedBike[] mobai = new SharedBike[20];
        SharedBike mobai0 = new SharedBike(1, "摩拜单车0", 1, null);
        SharedBike mobai1 = new SharedBike(2, "摩拜单车1", 1, null);
        SharedBike mobai2 = new SharedBike(3, "摩拜单车2", 1, null);
        mobai[0] = mobai0;
        mobai[1] = mobai1;
        mobai[2] = mobai2;
        SharedBikeCompany companymobai = new SharedBikeCompany(3, "摩拜单车", mobai, 3, 50);

        companies[0] = companyofo;
        companies[1] = companyhalo;
        companies[2] = companymobai;
    }

    public void start() {
        startMeau();
    }


    public void startMeau() {
        System.out.println("              欢迎使用迷你共享单车");
        System.out.println("*********************************************");
        System.out.println("              1.投放Bike");
        System.out.println("              2.查看Bike");
        System.out.println("              3.删除Bike");
        System.out.println("              4.借出Bike");
        System.out.println("              5.归还Bike");
        System.out.println("              6.Bike排行榜");
        System.out.println("              7.退出");
        System.out.println("*********************************************");
        System.out.println("请选择:");
        int choice = input.nextInt();
        switch (choice) {
            case 1:
                putBike();
                break;
            case 2:
                showBike();
                break;
            case 3:
                deleteBike();
                break;
            case 4:
//                borrowBike();
                break;
            case 5:
//                retrunBike();
                break;
            case 6:
                rankBike();
                break;
            case 7:
                break;
        }

    }


    private void putBike() {
        System.out.println("1.投放Bike---->");
        for (int i = 0; i < companies.length; i++) {
            System.out.println((i + 1) + "." + companies[i].getBikeCompanyName());
        }
        System.out.println("请选择要投放的单车品牌");
        int bikeid = input.nextInt();
        System.out.println("请录入要投放的数量");
        int bikenum = input.nextInt();
        switch (bikeid) {
            case 1:
                putBikes(bikeid, bikenum);
                break;
            case 2:
                putBikes(bikeid, bikenum);
                break;
            case 3:
                putBikes(bikeid, bikenum);
                break;
        }

        System.out.println("-----------------------");
        returnMain();
    }


    private void putBikes(int companyid, int newbikenum) {

        if (companyid == 1) {
            int len = companies[0].getSharedBikes().length;
            int count = 0;
            for (int i = 0; i  < len; i ++) {
                if(companies[0].getSharedBikes()[i]!=null){
                    count++;
                }
            }

            System.out.println(count);
//            for(int j = 0 ;j<=newbikenum;j++){
                companies[0].getSharedBikes()[3].setBid(count+1);
                companies[0].getSharedBikes()[3].setBname("ofo单车"+count);
                companies[0].getSharedBikes()[3].setStatus(1);
                companies[0].getSharedBikes()[3].setBorrowTime(null);
                count++;
//            }
        } else if (companyid == 2) {

        } else if (companyid == 3) {

        }

    }

    private void rankBike() {
        System.out.println("------>6.排行榜");
        SharedBikeCompany[] newCompany = new SharedBikeCompany[companies.length];
        for (int i = 0; i < newCompany.length; i++) {
            if (companies[i] != null) {
                newCompany[i] = companies[i];
            }
        }
        int len = newCompany.length;
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len - i - 1; j++) {
                if (newCompany[i].getCount() < newCompany[i + 1].getCount()) {
                    SharedBikeCompany temp = newCompany[i + 1];
                    newCompany[i + 1] = newCompany[i];
                    newCompany[i] = temp;
                }
            }
        }
        System.out.println("公司名称" + "\t\t\t" + "借车次数");
        for (int i = 0; i < len; i++) {
            if (newCompany[i].getCount() != null) {
                System.out.println(newCompany[i].getBikeCompanyName() + "\t\t\t" + newCompany[i].getCount());
            }

        }
        returnMain();
    }


    private void deleteBike() {


    }


    private void showBike() {

        for (int i = 0, len = companies.length; i < len; i++) {
            System.out.println("+----------+---------+--------------------+----------------+");
            System.out.println("| 公司序号 | 公司名称 |   公司单车数量      |  公司借出次数   |");

            System.out.println("|   " + companies[i].getBikeCompangId() + "\t|" + companies[i].getBikeCompanyName() + "\t|\t\t\t" + companies[i].getSum() + "\t\t|\t\t" + companies[i].getCount());
            System.out.println("+----------+---------+--------------------+----------------+");
            System.out.println("单车序号   单车品牌    单车状态    借出时间");
            for (int i1 = 0, len1 = companies[i].getSharedBikes().length; i1 < len1; i1++) {
                    if(companies[i].getSharedBikes()[i1]==null){
                        break;
                    }
                SharedBike sharedBike = companies[i].getSharedBikes()[i1];
                String status = (sharedBike.getStatus() == 1) ? "可借" : "已借出";
                String borrowTime = (sharedBike.getBorrowTime() == null) ? "_" : sharedBike.getBorrowTime();
                System.out.println("\t" + companies[i].getSharedBikes()[i1].getBid() + "\t| " + companies[i].getSharedBikes()[i1].getBname() + "\t|\t" + status + "\t\t|" + borrowTime);

            }
            System.out.println("+----------+---------+--------------------+----------------+");
        }
        returnMain();
    }

    public void returnMain() {
        System.out.println("返回主菜单/退出（0/1）:");
        int choice = input.nextInt();
        if (choice == 0) {
            startMeau();
            return;
        } else if (choice == 1) {
            System.out.println("程序已经结束");
            return;
        } else {
            System.out.println("输入有误，请重新输入");
            startMeau();
            return;
        }
    }
}
